#include "./cpp-httplib-v0.7.15/httplib.h"
#include "./searcher.hpp"
#include "./log.hpp"

static const std::string root_path = "./wwwroot";
static const std::string input_src_path = "./data/raw_html/raw.txt";

int main()
{
    ns_searcher::searcher searcher;
    searcher.init_searcher(input_src_path);

    httplib::Server server;

    server.set_base_dir(root_path.c_str());

    server.Get("/s", [&searcher](const httplib::Request& req, httplib::Response& rsp)->void
            {
                if (!req.has_param("word"))
                {
                    rsp.set_content("必须要有搜索关键字!", "text/plain; charset=utf-8");
                    return;
                }

                std::string word = req.get_param_value("word");
                LOG(NARMAL,"用户正在搜索 : " + word);
                std::string json_string;
                searcher.search(word, &json_string);
                rsp.set_content(json_string, "application/json");
            });

    LOG(NARMAL,"正在监听中......");
    server.listen("0.0.0.0", 9999);

    return 0;
}
